<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <title>工厂模式</title>
</head>
<body>
    <button>进店开吃</button>
</body>
</html>
<script>
  // let factory = function(role){
  //     function superman(){
  //       this.name = '超级管理员'
  //       this.role = ['修改密码','发布消息','查看主页']
  //     }
  

  //     function commonMan(){
  //       this.name= '普通游客'
  //       this.role = '查看主页'
  //     }

  //     switch(role){
  //       case 'superman':
  //           return new superman();
  //           break;
  //       case 'man':
  //           return new commonMan();
  //           break
  //     }
  // }
  // let superman = factory('superman')
  // let man = factory('man')
</script>
<script>
//  function CarMaker(){}
 
//  CarMaker.prototype.drive=function(){
//      return "I have "+this.doors+" doors";
//  }

//  CarMaker.factory=function(type){
//      var constr=type,
//              newcar;
//      console.log(type,typeof  CarMaker[constr])
//      if(typeof CarMaker[constr]!=="function"){

//          throw{
//              name:"Error",
//              message:constr+"doesn't exist"
//          };
//          if(typeof CarMaker[constr].prototype.drive!=="function"){
//           console.log(typeof CarMaker[constr].prototype.drive)
//              CarMaker[constr].prototype = new CarMaker();
//          }
//          newcar=new CarMaker[constr]();
//          return newcar;
//      }
//  }
//  CarMaker.Compact= function () {
//      this.doors=4;
//  }
//  CarMaker.SUV= function () {
//      this.doors=24;
//  }
// let all = function(){
//   console.log('test')
// }
// let a = CarMaker.factory('Compact')
// console.log(a)
var Interface = function(name, methods) {
    if(arguments.length != 2) {
        throw new Error("请确认要检查的接口所传的参数是否正确，例如：var Person = new Interface('Person', ['GetName','GetAge']);");
    }
    if(methods.length == 0){
        throw new Error("要检查的接口的方法名不能为空");
    }
    this.Name = name;
    this.Method = [];
    for(var i = 0; i < methods.length; i++) {
        if(typeof methods[i] !== 'string') {
            throw new Error("方法名不是字符串");
        }
        this.Method.push(methods[i]);
    }
}
/*static method in interface*/
Interface.ensureImplement = function(object) {
    if(arguments.length < 2) {
        throw new Error("没有接口或实例");
    }

    for(var i = 1; i < arguments.length; i++) {
        var interface1 = arguments[i];
        if(interface1.constructor !== Interface) {
            throw new Error("参数不是接口");
        }
        for(var j = 0; j < interface1.Method.length; j++) {
            var method = interface1.Method[j];
            if(!object[method] || typeof object[method] !== 'function') {
                throw new Error("您的实例没有实现接口:\t" + method);
            }
        }
    }
}
</script>

<script>

  // 封装接口的类，假如该类中实现的  add del 2个方法
function Peson(){}
Peson.prototype.add = function(){
    console.log('新增接口');
}
Peson.prototype.del = function(){
    console.log('删除接口');
}

//在你使用该类的方法的时候先去检查你要用到的方法是否存在
var peson = new Peson();
//Interfaces
var check = new Interface('check',['add', 'del','class']);
//检查你要用到的方法是否存在，如果你要用的方法没有实现，会抛出错误提示
Interface.ensureImplement(peson, check);
//使用接口
// peson.add();
// peson.del();
</script>

<script>
  var PetShop = function(){}
  PetShop.prototype = {
    sellPet:function(kind){
       var pet;
       switch(kind){
          case 'dog':
             pet = new Dog();
             break;
          case 'cat':
             pet  = new Cat();
             break;
          case 'pig':
             pet = new Pig()
             break;
          default:
             pet = new Bird()
       }

      Interface.ensureImplement(pet,)
    }
  }
</script>
